<?php

namespace app\api\controller\permission;

use app\api\controller\common\ApiBase;
use think\Db; 
use think\Controller; // dengh quxiao
class Quitcompany extends ApiBase
{
    public function getTabName($exhibition_id){
        $result=db('exhibition')->where(['id'=>$exhibition_id])->find();
        $tabName="";
        if($result){
            $tabName=$result['create_tablename'];
        }else{
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        return $tabName;
     }

    /**
     * 查看角色
     *
     * @return void
     */
    public function index()
    {
        if (request()->isPost()) {
            $exhibiton_id =$this->request->param('exhibiton_id');
            $page = $this->request->param('page');
            $keyword = $this->request->param('keyword');
            $member = $this->request->param('member');
            if(!$member){
                return json(array('code' => '0', 'msg' => '业务员数据错误'));
            }
            $page = max(1,intval($page));
            $pagesize =$this->request->param('pageSize')==""?20:$this->request->param('pageSize');
            $tabName=$this->getTabName($exhibiton_id);
            $param='';
            if (!empty($keyword)) {
                $param.=" and b.company_name like '%$keyword%'";
            }
            $sql2="SELECT a.realname,b.* FROM crm_".$tabName." b LEFT JOIN crm_admin a ON b.member_id=a.id";
            $sql2.=" where 1=1 $param AND b.member_id=".$member." ORDER BY b.id ";
            $sql2.=" limit ".($page-1)*$pagesize.",$pagesize";
            $list=Db::query($sql2);
            $sql21=" select count(b.id) as count1 ";
            $sql21.=" from crm_".$tabName." as b where 1=1 AND b.member_id=".$member." $param ";
            $total1=Db::query($sql21);
            $total=$total1[0]['count1']; //st
            return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'total'=>$total]]);
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    /**
     * 一键分配
     *
     * @return void
     */
    public function giveOnes()
    {
        if (request()->isPost()) {
            $exhibiton_id =$this->request->param('exhibiton_id');
            $member_id = $this->request->param('member_id');
            $age_member_id = $this->request->param('age_member_id');
            $admins = session('admin');
            if(!$exhibiton_id){
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $admin=db('admin')->where(['id'=>$age_member_id])->find();
            if(empty($admin)){
                return json(array('code' => '0', 'msg' => '接收人员信息错误'));
            }
            $admin1=db('admin')->where(['id'=>$member_id])->find();
            if(empty($admin1)){
                return json(array('code' => '0', 'msg' => '被分配人信息错误'));
            }
            $tabName=$this->getTabName($exhibiton_id);
            $list=db($tabName)->where(['member_id'=>$member_id])->select();
            $ids=[];
            foreach ($list as $k =>&$v){
                $ids[]=$v['id'];
            }
            if(empty($ids)){
                return json(array('code' => '0', 'msg' => '暂无可分配展商'));
            }
            $res=db($tabName)->where(['id'=>['in',implode(',',$ids)]])->update(['member_id'=>$age_member_id,'sale_id'=>$admins['id']]);
            if(!$res){
                return json(array('code' => '0', 'msg' => '操作失败'));
            }
            return json(array('code' => '1', 'msg' => '操作成功'));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    /**
     * 批量分配
     *
     * @return void
     */

    public function giveLarge()
    {
        if (request()->isPost()) {
            $exhibiton_id =$this->request->param('exhibiton_id');
            $age_member_id = $this->request->param('age_member_id');
            $ids=$this->request->param('ids');
            $admins = session('admin');
            if(!$exhibiton_id){
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            if(!$ids){
                return json(array('code' => '0', 'msg' => '请选择展商'));
            }
            $admin=db('admin')->where(['id'=>$age_member_id])->find();
            if(empty($admin)){
                return json(array('code' => '0', 'msg' => '接收人员信息错误'));
            }
            $tabName=$this->getTabName($exhibiton_id);
            if(empty($ids)){
                return json(array('code' => '0', 'msg' => '暂无可分配展商'));
            }
            $res=db($tabName)->where(['id'=>['in',$ids]])->update(['member_id'=>$age_member_id,'sale_id'=>$admins['id']]);
            if(!$res){
                return json(array('code' => '0', 'msg' => '操作失败'));
            }
            return json(array('code' => '1', 'msg' => '操作成功'));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


     /**
     * 退回公海
     *
     * @return void
     */

    public function returnLarge()
    {
        if (request()->isPost()) {
            $exhibiton_id =$this->request->param('exhibiton_id');
            $id = $this->request->param('id');
            if(!$exhibiton_id){
                return json(array('code' => '0', 'msg' => '展会信息错误'));
            }
            $tabName=$this->getTabName($exhibiton_id);
            $res=db($tabName)->where(['id'=>$id])->find();
            if(empty($res)){
                return json(array('code' => '0', 'msg' => '展商信息错误'));
            }
            if($res['status']>3){
                return json(array('code' => '0', 'msg' => '展商状态为签约以上,不可退回'));
            }
            $data['back'] = 1;
            $data['status'] = 0;
            $data['sale_id'] = 0;
            $data['member_id'] = 0;
            $r = db($tabName)->where(['id'=>$id])->update($data);
            if(!$r){
                return json(['code'=>0,'msg'=>'操作失败，请重试']);
            }
            return json(array('code' => '1', 'msg' => '操作成功'));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

}
